package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.my.myorders.MyOrderAddReq;
import com.black.app.domain.store.AppStoreOrderDetails;
import com.black.app.mapper.store.AppStoreOrderDetailsMapper;
import com.black.app.service.store.AppStoreOrderDetailsService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author admin
* @description 针对表【app_store_order_details】的数据库操作Service实现
* @createDate 2024-10-29 09:39:38
*/
@Service
public class AppStoreOrderDetailsServiceImpl extends ServiceImpl<AppStoreOrderDetailsMapper, AppStoreOrderDetails>
    implements AppStoreOrderDetailsService{

    /**
     * 添加订单明细
     * @param req
     * @return
     */
    @Override
    public boolean addOrderDetails(MyOrderAddReq req) {
        AppStoreOrderDetails details = new AppStoreOrderDetails();

        details.setOrderId(req.getOrderId());
        details.setOrderCode(req.getOrderCode());
        details.setSpecificationId(req.getSpecificationId());
        details.setProductId(req.getProductId());
        details.setCouponId(req.getCouponId());
        details.setPurchasingPrice(req.getTotalAmount());
        details.setPurchaseQuantity(req.getTotalQuantity());
        details.setSpecificationImageUrl(this.getUrl(req.getSpecificationId()));
        details.setProductName(this.getProductName(req.getProductId()));

        return save(details);
    }

    /**
     * 根据订单编号删除订单明细
     * @param orderCodes
     * @return
     */
    @Override
    public boolean deleteByCode(String[] orderCodes) {
        if(Objects.isNull(orderCodes) || orderCodes.length == 0){
            throw new RuntimeException("订单编号不存在");
        }
        Integer[] ids = new Integer[orderCodes.length];
        for (int i=0;i<orderCodes.length;i++){
            ids[i] = this.baseMapper.getDetailsId(orderCodes[i]);
        }
        for (int i=0;i<orderCodes.length;i++){
            AppStoreOrderDetails details = this.getById(ids[i]);
            if(Objects.nonNull(details)){
                this.removeById(ids[i]);
            }
        }

        return true;
    }

    private String getProductName(Integer productId) {
        return this.baseMapper.selectProductName(productId);
    }

    private String getUrl(Integer specificationId) {
        return this.baseMapper.selectUrl(specificationId);
    }
}




